package com.shievy.anweb.webview.web.activity

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.shievy.anweb.common.utli.visibility
import com.shievy.anweb.webview.R
import com.shievy.anweb.webview.web.callback.WebResultCallBack
import com.shievy.anweb.webview.databinding.ActivityWebViewFragmentBinding
import com.shievy.anweb.webview.uitl.Constants

/**
 * 测试Fragment的activity
 * */
class WebViewFragmentActivity : AppCompatActivity() , WebResultCallBack {
    private val binding: ActivityWebViewFragmentBinding by lazy {
        DataBindingUtil.setContentView(
            this,
            R.layout.activity_web_view_fragment
        )
    }
    private val url: String by lazy { intent?.extras?.getString(Constants.WEB_URL, "") ?: "" }
    private val title: String by lazy {
        intent?.extras?.getString(Constants.WEB_BAR_TITLE, "") ?: ""
    }
    private val isShowBar: Boolean by lazy {
        intent?.extras?.getBoolean(
            Constants.WEB_BAR_IS_SHOW,
            false
        ) ?: false
    }


    @SuppressLint("CommitTransaction")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.flBar.visibility(isShowBar)
        if (isShowBar) {
            binding.tvTitle.text = title
            binding.ivClose.setOnClickListener {
                finish()
            }
        }
        supportFragmentManager.apply {
            beginTransaction().replace(
                R.id.fl_web,
                WebViewFragment.createWebViewFragment(url, intent.extras ?: Bundle())
            ).commit()
        }
    }

    override fun onChangeTitle(title: String?) {
        if (isShowBar) {
            title?.let { binding.tvTitle.text = it }
        }
    }
}